package com.gullmall.product.service.impl;

import com.gullmall.common.utils.R;
import com.gullmall.product.entity.AttrEntity;
import com.gullmall.product.service.AttrService;
import com.gullmall.product.vo.spu.BaseAttrs;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gullmall.common.utils.PageUtils;
import com.gullmall.common.utils.Query;

import com.gullmall.product.dao.ProductAttrValueDao;
import com.gullmall.product.entity.ProductAttrValueEntity;
import com.gullmall.product.service.ProductAttrValueService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;


@Service("productAttrValueService")
@Transactional
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueDao, ProductAttrValueEntity> implements ProductAttrValueService {

    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductAttrValueEntity> page = this.page(
                new Query<ProductAttrValueEntity>().getPage(params),
                new QueryWrapper<ProductAttrValueEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * @param baseAttrs 只有字段  attrId; attrValues;  showDesc;
     * @param spuId
     * @return
     */
    @Override
    public Boolean saveBaseAttrs(List<BaseAttrs> baseAttrs, Long spuId) {
        if (!CollectionUtils.isEmpty(baseAttrs)){
            List<ProductAttrValueEntity> productAttrValueEntities = baseAttrs.stream().map(attr -> {
                ProductAttrValueEntity pave = new ProductAttrValueEntity();
                //BaseAttrs中的showDesc 对应 ProductAttrValueEntity 中的 quickShow 故要单独设置
                pave.setQuickShow(attr.getShowDesc());
                pave.setSpuId(spuId);
                //BaseAttrs中的attrValues 对应 ProductAttrValueEntity 中的 attrValue 故要单独设置
                pave.setAttrValue(attr.getAttrValues());
                pave.setAttrId(attr.getAttrId());

                //属性名前端不会传来,要单独查
                AttrEntity attrById = attrService.getById(attr.getAttrId());
                pave.setAttrName(attrById.getAttrName());
                return pave;
            }).collect(Collectors.toList());
            return this.saveBatch(productAttrValueEntities);
        }
        return false;
    }

    @Override
    public R updateBySpuId(Long spuId, List<ProductAttrValueEntity> productAttrValueEntityList) {
        //1,直接删除
        //2,重新新增
        this.remove(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id",spuId));
        List<ProductAttrValueEntity> updater = productAttrValueEntityList.stream().map(m -> {
            m.setSpuId(spuId);
            return m;
        }).collect(Collectors.toList());
        this.saveBatch(updater);
        return R.ok();
    }

}